﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace SignalTable
{
    class Utils
    {
        public static double[,] LoadFile(string fileName, out string headerLine)
        {
            StreamReader sr = new StreamReader(fileName);
            string theLine;
            headerLine = sr.ReadLine();
            string[] Headers = headerLine.Split(new string[] { " ", ";" }, StringSplitOptions.RemoveEmptyEntries);
            int numAttributes = Headers.Length;
            int numVectors = 0;
            while ((theLine = sr.ReadLine()) != null)
                if (theLine.Trim().Length > 2)
                    numVectors++;
            sr.Close();
            double[,] DataSet = new double[numVectors, numAttributes];

            int v = 0;
            sr = new StreamReader(fileName);
            theLine = sr.ReadLine();
            unsafe
            {
                fixed (double* p3 = &DataSet[0, 0])
                {
                    while ((theLine = sr.ReadLine()) != null)
                    {
                        if (theLine.Trim().Length > 2)
                        {
                            string[] S = theLine.Split(new string[] { " ", ";" }, StringSplitOptions.RemoveEmptyEntries);
                            int iv = v * DataSet.GetLength(1);
                            for (int a = 0; a < numAttributes; a++)
                            {
                                *(p3 + iv + a) = Double.Parse(S[a], System.Globalization.CultureInfo.InvariantCulture);
                            }
                            v++;
                        }
                    }
                }
            }
            sr.Close();
            return DataSet;
        }
                            

    }
}


